<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="description" content="">
    <meta name="author" content="">
    <title>商品详情</title>
    <!-- JQuery -->
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <!-- Bootstrap核心CSS和JS -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script>
        var login=false;
        $(function () {
            getInfo();
            $("#btn_submit").click(function () {
                if(login)
                location.href = window.location.href + "/kill";
                else {
                    alert("请登录");
                    location.href="/login";
                }
            });
            /* 获取用户信息 */
            $.getJSON(
                "/user/getInfo",
                {},
                function (json) {
                    if (json.statusCode !== 200) {

                    } else {
                        login=true;
                        $("#nav_login").html(`
                        <a href="/space">
                        <span class="glyphicon glyphicon-user"></span>
                        <span id="userName">${json.data.realname}</span></a>
                        `);

                        $("#nav_register").html(`
                    <a href="/user/signOut">安全退出</a>
                    `);

                    }
                },
                "json"
            );
        });

        var remainSeconds = 0;
        var killStatus = 0;
        var setTime;

        /* 搜索 */
        function search() {
            let key = $("#key").val();
            location.href = "/goods/search/" + key;
        }

        function timeSed() {
            remainSeconds = remainSeconds - 1;
            if (remainSeconds<=0) {
                remainSeconds = 0;
                clearTimeout(setTime);
                getInfo();
            } else {
                $("#time").html(remainSeconds + "秒");
                setTime = setTimeout(timeSed, 1000);
            }
        }

        /* 获取信息 */
        function getInfo() {
            $.getJSON(
                window.location.href + "/getGoods",
                {},
                function (json) {
                    if (json.statusCode !== 200) {
                        alert("获取失败！" + json.msg);
                    }
                    let killGoods = json.data.killGoods;
                    remainSeconds = json.data.remainSeconds;
                    killStatus = json.data.killStatus;

                    /* 设置状态 */
                    if (killStatus === 0) {
                        $("#status").html("距离开始还有：")
                        $("#time").html(remainSeconds + "秒");
                        $("#btn_submit").attr('disabled', true);
                    } else if (killStatus === 1) {
                        $("#status").html("距离结束还有：")
                        $("#time").html(remainSeconds + "秒");
                        $("#btn_submit").attr('disabled', false);
                    } else {
                        remainSeconds = -1;
                        $("#status").html("");
                        $("#time").html("秒杀已结束");
                        $("#btn_submit").attr('disabled', true);
                    }

                    /* 写入商品详情 */
                    if (killGoods.image == null || killGoods.image === "")
                        killGoods.image = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596716481337&di=fab03d71304d4827ecde2e1031b53a8e&imgtype=0&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Ffc1f4134970a304e9b7f6625d4c8a786c8175c94.jpg";
                    $("#goods_img").attr("src",killGoods.image);
                    $("#killId").html(killGoods.killId);
                    $("#goodsID").html(killGoods.goodsId);
                    $("#name").html(killGoods.name);
                    $("#goodsName").html(killGoods.name);
                    $("#price").html(killGoods.price/100);
                    $("#killPrice").html("秒杀价:" + killGoods.killPrice/100);
                    $("#num").html(killGoods.stockCount);
                    $("#goodsClass").html(killGoods.categoryId);

                    /* 未结束就创建定时器 */
                    if (killStatus !== 2)
                        timeSed();
                },
                "json"
            );
        }
    </script>
</head>
<body>
<div class="container">

    <!-- 导航 -->
    <nav class="navbar navbar-default" role="navigation">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1"><span
                    class="sr-only">WTUShop导航栏</span><span class="icon-bar"></span><span
                    class="icon-bar"></span><span class="icon-bar"></span></button>
            <a class="navbar-brand" href="/">WTUShop</a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <!-- 左边 -->
            <ul class="nav navbar-nav">
                <li><a href="/goods/search">商场</a></li>
                <li class="active"><a href="/kill/list">秒杀活动</a></li>
                <li><a href="/space/">个人中心</a></li>
                <li><a href="/admin/manage">控制台</a></li>
            </ul>
            <!-- 右边 -->
            <ul class="nav navbar-nav navbar-right">
                <li><a href="/shopping/list">购物车</a></li>
                <li><a href="/order/list">订单查询</a></li>
                <li id="nav_login"><a href="/login"><span
                        class="glyphicon glyphicon-user"></span><span id="userName">请登录</span></a></li>
                <li id="nav_register"><a href="/register">注册</a></li>
                <li>&nbsp;&nbsp;</li><!-- 占位符 -->
            </ul>
        </div>

    </nav>

    <!-- 标题和搜索 -->
    <div class="row clearfix page-header">
        <div class="col-md-8 column" >
            <a href="/"><h1>WTUShop</h1></a>
        </div>

        <div class="input-group col-md-4 column " style="margin-top: 20px">
            <input type="text" class="form-control" placeholder="输入关键字" id="key">
            <span class="input-group-btn">
                <a href="javaScript:search()">
                <button id="btn_search" class="btn btn-default">
                    <span class="glyphicon glyphicon-search"></span> 搜索
                </button>
                    </a>
                    &nbsp;
            </span>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <ul class="breadcrumb">
                <li>
                    <a href="/goods/search">商品列表</a>
                </li>
                <li>
                    <a href="/kill/list">秒杀活动</a>
                </li>
                <li class="active" id="goodsName">
                    正在加载中ing...
                </li>
            </ul>

            <!-- 主体 -->
            <div class="row clearfix">

                <!-- 左边 -->
                <div class="col-md-6 column text-right">
                    <img alt="预览图"
                         src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596716481337&di=fab03d71304d4827ecde2e1031b53a8e&imgtype=0&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Ffc1f4134970a304e9b7f6625d4c8a786c8175c94.jpg"
                         style="width: 100%;" class="img-rounded" id="goods_img"/>
                    <div class="panel panel-danger" style="margin-top: 10px">
                        <div class="panel-heading">
                            <h3 class="panel-title">
                                <span style="font-size: 16px">原价：</span>
                                <s style="color: green;font-size: 16px" id="price">????</s>&nbsp;&nbsp;
                                <span style="color: red;font-size: 24px" id="killPrice">秒杀价：????</span>
                            </h3>
                        </div>
                        <div class="panel-body">
                            <span style="font-size: 20px" id="status">距离开始</span>
                            <span style="color:cornflowerblue;font-size: 24px" id="time">-1秒</span>
                        </div>
                    </div>
                </div>

                <!-- 右边 -->
                <div class="col-md-6 column">
                    <div class="panel panel-info">
                        <div class="panel-heading">
                            <span style="font-size: 26px" id="name">
                                ????视频名称正在加载中ing.....
                            </span>
                        </div>
                        <div class="panel-body ">
                            <span style="font-size: 22px">剩余数量：</span>
                            <span style="font-size: 26px;color: red" id="num">???</span>
                        </div>
                    </div>

                    <dl class="dl-horizontal">

                        <dt>商品ID：</dt>
                        <dd id="goodsID">????</dd>

                        <dt>活动ID：</dt>
                        <dd id="killId">????</dd>

                        <dt>商品类别：</dt>
                        <dd id="goodsClass">????</dd>

                        <dt>详情信息：</dt>
                        <dd>
                            这是WTUShop很多商品中的一个，你买不了吃亏，买不了上当，虽然你也买不了。
                            你问我为啥这里写怎么多废话，那不是废话吗？显然是的，不然这么大的地方你空着，它好看吗？
                        </dd>

                    </dl>
                    <button id="btn_submit" type="button" class="btn btn-default btn-info btn-block">立即秒杀</button>
                </div>

            </div>

        </div>

    </div>

</div>
</body>
</html>